import { ElMessage, ElMessageBox } from 'element-plus'

import Video from 'video.js'
import 'video.js/dist/video-js.min.css'
// import { useStore } from 'vuex'
import store from '@/store'

import moment from 'moment'

export default () => {
  const $video = Video

  // const store = useStore()
  // 格式化时间
  const $format = (val: string | Date, format?: string) => {
    if (!val) return '-'
    if (!format) return moment(val).format('YYYY-MM-DD HH:mm:ss')
    format = format.replace('Y', 'YYYY')
    format = format.replace('M', 'MM')
    format = format.replace('D', 'DD')
    format = format.replace('H', 'HH')
    format = format.replace('m', 'mm')
    format = format.replace('s', 'ss')
    return moment(val).format(format)
  }

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  // 按钮权限
  const $btnPerms = function (perms) {
    let hasPermission = false
    if (!perms) return hasPermission
    const permission = (store.state as any).permission.btnPerms
    hasPermission = permission.some(item => item.permissions === perms)
    return hasPermission
  }

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  // treeData格式化
  const $formatTree = (tree, keys = { label: 'name', value: 'id', children: 'children' }) => {
    tree = tree.map(node => {
      if (node.children && node.children.length) {
        node.children = $formatTree(node.children, keys)
      } else {
        delete node.children
      }
      const obj = {}
      for (const key in keys) {
        obj[key] = node[keys[key]]
      }
      return obj
    })

    return tree
  }

  const $confirm = ElMessageBox.confirm

  const $message = ElMessage

  return {
    $message,
    $confirm,
    $formatTree,
    $format,
    $video,
    $btnPerms
  }
}
